package validator;

import exception.ParamCheckFailureException;

/**
 * String类型的校验器
 * 实现：
 *
 * @author shishaolong
 * @datatime 2021/1/4 9:50
 */
public class StringValidator extends AbstractValidator {

    private String paramValue;
    private String paramName;

    /**
     * 构造器
     *
     * @param paramValue 参数值
     * @param paramName  参数名
     */
    public StringValidator(String paramValue, String paramName) {
        this.paramValue = paramValue;
        this.paramName = paramName;
    }

    /**
     * 获取参数名
     *
     * @return
     */
    @Override
    protected String getParamName() {
        return this.paramName;
    }

    /**
     * 获取参数值
     *
     * @return
     */
    @Override
    protected Object getParamValue() {
        return this.paramValue;
    }

    @Override
    public StringValidator min(int min, String errorMessage) throws ParamCheckFailureException {
        if (paramValue.length() < min) {
            throw createPcfException(errorMessage);
        }
        return this;
    }

    @Override
    public StringValidator max(int max, String errorMessage) throws ParamCheckFailureException {
        if (paramValue.length() > max) {
            throw createPcfException(errorMessage);
        }
        return this;
    }
}
